﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlServerCe;

namespace RefOffPDA
{
    /*
     * Class: CautionCode
     * 
     * Use: Form used to select the disciplinary code when a
     *      caution is given
     *      
     * Author: Mike Skaife, mcs3@aber.ac.uk
     */ 
    public partial class CautionCode : Form
    {
        //Details required to connect
        //to the database
        string conString;
        SqlCeConnection conn;

        //The current Match object
        Match theMatch;

        //Indicates whether the home or away
        //team is receiving the caution
        public String homeOrAway;

        //The id of the current Match within
        //the database
        int matchId;

        //Buttons from other screens which need to be 
        //updated as a result of the caution, by being
        //coloured yellow
        public Button sendOffButton;
        public Button goalButton;
        public Button cautionButton;
        public Button subOffButton;

        //The id of the player receiving the caution
        public int player_id;

        //Used to temporarily store the chosen disciplinary
        //code for use in database query
        String code;

        public CautionCode(Match match)
        {
            InitializeComponent();
            theMatch = match;
        }

        //This method is used to record the actual caution within
        //the database
        public void cautionPlayer()
        {
            //Obtain the current GPS location of the PDA to
            //indicate location of incident later on Virtual
            //Pitch
            String gps = getGPS();

            //Connect to the database
            connectToDatabase();

            //Set id of current Match for use in query
            matchId = theMatch.match_id;

            //Current value of match timer, to indicate when incident occurred
            String timeOfIncident = theMatch.tenMinutes + theMatch.minutes + ":" + theMatch.tenSeconds + theMatch.seconds;
            
            //Create and execute SQL command to record the caution in the database
            //using the required parameters
            string command = @"INSERT INTO caution(caution_code,time,player_id,match_id,incident_GPS) VALUES(@code,@time,@id,@match,@incidentGPS);";
            using (SqlCeCommand caution_player = new SqlCeCommand(command, conn))
            {
                caution_player.Parameters.AddWithValue("@code", code);
                caution_player.Parameters.AddWithValue("@time", timeOfIncident);
                caution_player.Parameters.AddWithValue("@id", player_id);
                caution_player.Parameters.AddWithValue("@match", matchId);
                caution_player.Parameters.AddWithValue("@incidentGPS", gps);
                caution_player.ExecuteNonQuery();
            }

            //Disconnect from the database
            closeDatabaseConnection();
        }

        //This method is used to record a caution in the database if this
        //is the second caution being given to a player, meaning the player
        //also has to be sent off
        public void cautionPlayerOff()
        {
            //Obtaing the current GPS location of the PDA to
            //indicate location of incident later on Virtual
            //Pitch
            String gps = getGPS();

            //Connect to the database
            connectToDatabase();

            //Set id of current match for use in query
            matchId = theMatch.match_id;

            //Current value of match timer used to indicate
            //when the incident occurred
            String timeOfIncident = theMatch.tenMinutes + theMatch.minutes + ":" + theMatch.tenSeconds + theMatch.seconds;
            
            //Create and execute SQL command to record caution in database
            string command = @"INSERT INTO caution(caution_code,time,player_id,match_id,incident_GPS) VALUES(@code,@time,@id,@match,@incidentGPS);";
            using (SqlCeCommand caution_player_off = new SqlCeCommand(command, conn))
            {
                caution_player_off.Parameters.AddWithValue("@code", code);
                caution_player_off.Parameters.AddWithValue("@time", timeOfIncident);
                caution_player_off.Parameters.AddWithValue("@id", player_id);
                caution_player_off.Parameters.AddWithValue("@match", matchId);
                caution_player_off.Parameters.AddWithValue("@incidentGPS", gps);
                caution_player_off.ExecuteNonQuery();
            }

            //Close database connection
            closeDatabaseConnection();

            //Connect to database to record the resulting
            //sending off
            connectToDatabase();

            //Create and execute SQL command to record sending off, with the
            //code 'S7', used to indicate that a player has been sent off for
            //receiving their second caution within the same match
            string off_command = @"INSERT INTO sendoff(sendoff_code,time,player_id,match_id,incident_GPS) VALUES(@code,@time,@id,@match,@incidentGPS);";
            using (SqlCeCommand caution_player_off_off = new SqlCeCommand(off_command, conn))
            {
                caution_player_off_off.Parameters.AddWithValue("@code", "S7");
                caution_player_off_off.Parameters.AddWithValue("@time", timeOfIncident);
                caution_player_off_off.Parameters.AddWithValue("@id", player_id);
                caution_player_off_off.Parameters.AddWithValue("@match", matchId);
                caution_player_off_off.Parameters.AddWithValue("@incidentGPS", gps);
                caution_player_off_off.ExecuteNonQuery();
            }

            //Disconnect from the database
            closeDatabaseConnection();
        }

        //This method is run when the chosen disciplinary code is 'C1'
        private void CautionCodeC1Btn_Click(object sender, EventArgs e)
        {
            //Indicate in the C1 screen which buttons need to be
            //updated as a result of the caution
            theMatch.c1Code.cautionButton = cautionButton;
            theMatch.c1Code.goalButton = goalButton;
            theMatch.c1Code.sendOffButton = sendOffButton;
            theMatch.c1Code.subOffButton = subOffButton;

            //Indicate which player is receiving the caution,
            //in the C1 form
            theMatch.c1Code.player_id = player_id;


            //Hide this form and display the C1 screen, so the
            //relevant C1 code can then be chosen
            theMatch.c1Code.Visible = true;
            this.Visible = false;
        }

        //This method is run when the chosen disciplinary code is 'C2'
        private void CautionCodeC2Btn_Click(object sender, EventArgs e)
        {
            //Indicate the chosen disciplinary code
            code = "C2";

            //Find out if this is the players second caution, by
            //determining if the clicked button is already
            //coloured yellow
            if (!cautionButton.BackColor.Equals(Color.Yellow))
            {
                //If this is the players first caution, colour
                //in all relevant buttons on other screens to 
                //indicate caution has been recorded
                sendOffButton.BackColor = Color.Yellow;
                goalButton.BackColor = Color.Yellow;
                cautionButton.BackColor = Color.Yellow;
                subOffButton.BackColor = Color.Yellow;

                //Carry out the actual caution by
                //calling database method
                cautionPlayer();
            }
            //This code runs if this is the players
            //second caution
            else
            {
                //Inform the referee that the player is also being sent off
                MessageBox.Show("This player is being sent off","Second Caution",MessageBoxButtons.OK,MessageBoxIcon.Exclamation,MessageBoxDefaultButton.Button1);
                
                //Colour in all relevant buttons on other screens, to indicate
                //that sending off has occurred, by colouring them red and
                //disabling them
                sendOffButton.BackColor = Color.Red;
                sendOffButton.Enabled = false;
                goalButton.BackColor = Color.Red;
                goalButton.Enabled = false;
                cautionButton.BackColor = Color.Red;
                cautionButton.Enabled = false;
                subOffButton.BackColor = Color.Red;
                subOffButton.Enabled = false;

                //Record the caution and sending off in the database
                cautionPlayerOff();
            }

            //Hide this screen to display the main Match screen
            this.Visible = false;
        }

        //This method is run when the chosen disciplinary code is 'C3'
        private void CautionCodeC3Btn_Click(object sender, EventArgs e)
        {
            //Indicate the chosen disciplinary code
            code = "C3";

            //Find out if this is the players second caution, by
            //determining if the clicked button is already
            //coloured yellow
            if (!cautionButton.BackColor.Equals(Color.Yellow))
            {
                //If this is the players first caution, colour
                //in all relevant buttons on other screens to 
                //indicate caution has been recorded
                sendOffButton.BackColor = Color.Yellow;
                goalButton.BackColor = Color.Yellow;
                cautionButton.BackColor = Color.Yellow;
                subOffButton.BackColor = Color.Yellow;

                //Carry out the actual caution by
                //calling database method
                cautionPlayer();
            }
            //This code runs if this is the players
            //second caution            
            else
            {
                //Inform the referee that the player is also being sent off
                MessageBox.Show("This player is being sent off", "Second Caution", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);

                //Colour in all relevant buttons on other screens, to indicate
                //that sending off has occurred, by colouring them red and
                //disabling them
                sendOffButton.BackColor = Color.Red;
                sendOffButton.Enabled = false;
                goalButton.BackColor = Color.Red;
                goalButton.Enabled = false;
                cautionButton.BackColor = Color.Red;
                cautionButton.Enabled = false;
                subOffButton.BackColor = Color.Red;
                subOffButton.Enabled = false;

                //Record the caution and sending off in the database
                cautionPlayerOff();
            }

            //Hide this screen to display the main Match screen
            this.Visible = false;
        }

        //This method is run when the chosen disciplinary code is 'C4'
        private void CautionCodeC4Btn_Click(object sender, EventArgs e)
        {
            //Indicate the chosen disciplinary code
            code = "C4";

            //Find out if this is the players second caution, by
            //determining if the clicked button is already
            //coloured yellow
            if (!cautionButton.BackColor.Equals(Color.Yellow))
            {
                //If this is the players first caution, colour
                //in all relevant buttons on other screens to 
                //indicate caution has been recorded
                sendOffButton.BackColor = Color.Yellow;
                goalButton.BackColor = Color.Yellow;
                cautionButton.BackColor = Color.Yellow;
                subOffButton.BackColor = Color.Yellow;

                //Carry out the actual caution by
                //calling database method
                cautionPlayer();
            }
            //This code runs if this is the players
            //second caution
            else
            {
                //Inform the referee that the player is also being sent off
                MessageBox.Show("This player is being sent off", "Second Caution", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);

                //Colour in all relevant buttons on other screens, to indicate
                //that sending off has occurred, by colouring them red and
                //disabling them
                sendOffButton.BackColor = Color.Red;
                sendOffButton.Enabled = false;
                goalButton.BackColor = Color.Red;
                goalButton.Enabled = false;
                cautionButton.BackColor = Color.Red;
                cautionButton.Enabled = false;
                subOffButton.BackColor = Color.Red;
                subOffButton.Enabled = false;

                //Record the caution and sending off in the database
                cautionPlayerOff();
            }

            //Hide this screen to display the main Match screen
            this.Visible = false;
        }

        //This method is run when the chosen disciplinary code is 'C5'
        private void CautionCodeC5Btn_Click(object sender, EventArgs e)
        {
            //Indicate the chosen disciplinary code
            code = "C5";

            //Find out if this is the players second caution, by
            //determining if the clicked button is already
            //coloured yellow
            if (!cautionButton.BackColor.Equals(Color.Yellow))
            {
                //If this is the players first caution, colour
                //in all relevant buttons on other screens to 
                //indicate caution has been recorded
                sendOffButton.BackColor = Color.Yellow;
                goalButton.BackColor = Color.Yellow;
                cautionButton.BackColor = Color.Yellow;
                subOffButton.BackColor = Color.Yellow;

                //Carry out the actual caution by
                //calling database method
                cautionPlayer();
            }
            //This code runs if this is the players
            //second caution
            else
            {
                //Inform the referee that the player is also being sent off
                MessageBox.Show("This player is being sent off", "Second Caution", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);

                //Colour in all relevant buttons on other screens, to indicate
                //that sending off has occurred, by colouring them red and
                //disabling them
                sendOffButton.BackColor = Color.Red;
                sendOffButton.Enabled = false;
                goalButton.BackColor = Color.Red;
                goalButton.Enabled = false;
                cautionButton.BackColor = Color.Red;
                cautionButton.Enabled = false;
                subOffButton.BackColor = Color.Red;
                subOffButton.Enabled = false;

                //Record the caution and sending off in the database
                cautionPlayerOff();
            }

            //Hide this screen to display the main Match screen
            this.Visible = false;
        }

        //This method is run when the chosen disciplinary code is 'C6'
        private void CautionCodeC6Btn_Click(object sender, EventArgs e)
        {
            //Indicate the chosen disciplinary code
            code = "C6";

            //Find out if this is the players second caution, by
            //determining if the clicked button is already
            //coloured yellow
            if (!cautionButton.BackColor.Equals(Color.Yellow))
            {
                //If this is the players first caution, colour
                //in all relevant buttons on other screens to 
                //indicate caution has been recorded
                sendOffButton.BackColor = Color.Yellow;
                goalButton.BackColor = Color.Yellow;
                cautionButton.BackColor = Color.Yellow;
                subOffButton.BackColor = Color.Yellow;

                //Carry out the actual caution by
                //calling database method
                cautionPlayer();
            }
            //This code runs if this is the players
            //second caution
            else
            {
                //Inform the referee that the player is also being sent off
                MessageBox.Show("This player is being sent off", "Second Caution", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);

                //Colour in all relevant buttons on other screens, to indicate
                //that sending off has occurred, by colouring them red and
                //disabling them
                sendOffButton.BackColor = Color.Red;
                sendOffButton.Enabled = false;
                goalButton.BackColor = Color.Red;
                goalButton.Enabled = false;
                cautionButton.BackColor = Color.Red;
                cautionButton.Enabled = false;
                subOffButton.BackColor = Color.Red;
                subOffButton.Enabled = false;

                //Record the caution and sending off in the database
                cautionPlayerOff();
            }

            //Hide this screen to display the main Match screen
            this.Visible = false;
        }

        //This method is run when the chosen disciplinary code is 'C7'
        private void CautionCodeC7Btn_Click(object sender, EventArgs e)
        {
            //Indicate the chosen disciplinary code
            code = "C7";

            //Find out if this is the players second caution, by
            //determining if the clicked button is already
            //coloured yellow
            if (!cautionButton.BackColor.Equals(Color.Yellow))
            {
                //If this is the players first caution, colour
                //in all relevant buttons on other screens to 
                //indicate caution has been recorded
                sendOffButton.BackColor = Color.Yellow;
                goalButton.BackColor = Color.Yellow;
                cautionButton.BackColor = Color.Yellow;
                subOffButton.BackColor = Color.Yellow;

                //Carry out the actual caution by
                //calling database method
                cautionPlayer();
            }
            //This code runs if this is the players
            //second caution
            else
            {
                //Inform the referee that the player is also being sent off
                MessageBox.Show("This player is being sent off", "Second Caution", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);

                //Colour in all relevant buttons on other screens, to indicate
                //that sending off has occurred, by colouring them red and
                //disabling them
                sendOffButton.BackColor = Color.Red;
                sendOffButton.Enabled = false;
                goalButton.BackColor = Color.Red;
                goalButton.Enabled = false;
                cautionButton.BackColor = Color.Red;
                cautionButton.Enabled = false;
                subOffButton.BackColor = Color.Red;
                subOffButton.Enabled = false;

                //Record the caution and sending off in the database
                cautionPlayerOff();
            }

            //Hide this screen to display the main Match screen
            this.Visible = false;
        }

        //This method is used to record the GPS location
        //of the PDA device
        private String getGPS()
        {
            //Read the location from the GPS antenna
            String gps_location = theMatch.gpsReader.readGPS();

            //Return the GPS sentence as a String
            return gps_location;
        }

        //This method is used to connect to the database
        private void connectToDatabase()
        {
            // Retrieve the connection string
            conString = @"Data Source=\Program Files\RefOffPDA\RefOffPDA_DB.sdf";

            // Open the connection using the connection string
            conn = new SqlCeConnection(conString);
            conn.Open();
        }

        //This method is used to disconnect from the database
        private void closeDatabaseConnection()
        {
            //Close the database connection
            conn.Close();
        }

        //This method is run when the 'Cancel' button is clicked
        private void CancelButton_Click(object sender, EventArgs e)
        {
            //Hide this screen to display the main Match screen
            this.Visible = false;
        }
    }
}